package com.warning_speed_android.socket;

import java.net.SocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.prefixedstring.PrefixedStringCodecFactory;
import org.apache.mina.filter.logging.MdcInjectionFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Clientcommunicate {
	private final IoHandler handler;
	private IoSession session;
	private String fbbId;
	private String sessionId;

	public Clientcommunicate(IoHandler handler) {
		this.handler = handler;
	}

	public IoSession getSession() {
		return session;
	}

	public void setSession(IoSession session) {
		this.session = session;
	}

	public String getFbbId() {
		return fbbId;
	}

	public void setFbbId(String fbbId) {
		this.fbbId = fbbId;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public IoHandler getHandler() {
		return handler;
	}

	public boolean connect(NioSocketConnector connector, SocketAddress address,
			boolean useSsl) {
		if (session != null && session.isConnected()) {
			throw new IllegalStateException(
					"Already connected. Disconnect first.");
		}

		try {
			Charset charset = Charset.forName("UTF-8");

			PrefixedStringCodecFactory codecFactory = new PrefixedStringCodecFactory(
					charset);
			codecFactory.setEncoderMaxDataLength(2147483647);
			codecFactory.setDecoderMaxDataLength(2147483647);

			IoFilter CODEC_FILTER = (IoFilter) new ProtocolCodecFilter(
					codecFactory);
			connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
			connector.getFilterChain().addLast("codec", CODEC_FILTER);

			connector.getSessionConfig().setMaxReadBufferSize(5120);
			connector.setHandler(handler);
			ConnectFuture future1 = connector.connect(address);
			future1.awaitUninterruptibly();
			if (!future1.isConnected()) {
				return false;
			}
			session = future1.getSession();
			return true;
		} catch (Exception e) {
			Log.e(e.getMessage(), e.getMessage(), e);
			e.printStackTrace();
			return false;
		}

	}

	public void login(String text) throws JSONException {
		JSONObject object = new JSONObject();
		object.put("cmd", "loginSocket");
		object.put("token", text);
		session.write(object.toString());
	}

	public void location(String lat, String lon) throws JSONException {
		JSONObject object = new JSONObject();
		object.put("cmd", "location");
		JSONObject location = new JSONObject();
		location.put("lat", lat);
		location.put("lon", lon);
		object.put("location", location);
		object.put("token", sessionId);
		session.write(object.toString());
	}

//	public void startSendLocation(String typeVehicle) throws JSONException {
//		// TODO Auto-generated method stub
//		JSONObject object = new JSONObject();
//		object.put("cmd", "updateLocation");
//		object.put("typeVehicle", typeVehicle);
//		object.put("update", true);
//		object.put("token", sessionId);
//		session.write(object.toString());
//	}
//	
//	public void stopSendLocation() throws JSONException {
//		// TODO Auto-generated method stub
//		JSONObject object = new JSONObject();
//		object.put("cmd", "updateLocation");
//		object.put("update", false);
//		object.put("token", sessionId);
//		session.write(object.toString());
//	}

}
